package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysOrderMapper;
import com.ruoyi.system.service.ISysOrderService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-22
 */
@Service
public class SysOrderServiceImpl implements ISysOrderService 
{
    @Autowired
    private SysOrderMapper sysOrderMapper;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询订单
     * 
     * @param orderId 订单ID
     * @return 订单
     */
    @Override
    public Map<String, Object> selectSysOrderById(Long orderId)
    {
        SysOrder sysOrder = sysOrderMapper.selectSysOrderById(orderId);
        Map<String, Object> map = new HashMap<>();
        if (sysOrder != null) {
            SysUser sysUser = iSysUserService.selectUserById(sysOrder.getUserId());
            map.put("orderId", sysOrder.getOrderId());
            map.put("orderCreateDate", sysOrder.getOrderCreateDate());
            map.put("orderUpdateDate", sysOrder.getOrderUpdateDate());
            map.put("orderPrice", sysOrder.getOrderPrice());
            map.put("orderRemark", sysOrder.getOrderRemark());
            map.put("orderState", sysOrder.getOrderState());
            map.put("orderNum", sysOrder.getOrderNum());
            map.put("userId", sysOrder.getUserId());
            map.put("orderName", sysUser.getLoginName());
        }
        return map;
//        return sysOrderMapper.selectSysOrderById(orderId);
    }

    /**
     * 查询订单列表
     * 
     * @param sysOrder 订单
     * @return 订单
     */
    @Override
    public List<Map<String, Object>> selectSysOrderList(SysOrder sysOrder)
    {
        List<Map<String, Object>> list = new ArrayList<>();
        List<SysOrder> sysOrderList = sysOrderMapper.selectSysOrderList(sysOrder);
        if (sysOrderList != null) {
            for (SysOrder order : sysOrderList) {
                Map<String, Object> map = new HashMap<>();
                SysUser sysUser = iSysUserService.selectUserById(order.getUserId());
                map.put("orderId", order.getOrderId());
                map.put("orderCreateDate", order.getOrderCreateDate());
                map.put("orderUpdateDate", order.getOrderUpdateDate());
                map.put("orderPrice", order.getOrderPrice());
                map.put("orderRemark", order.getOrderRemark());
                map.put("orderState", order.getOrderState());
                map.put("orderNum", order.getOrderNum());
                map.put("orderName", sysUser.getLoginName());
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 查询订单列表
     *
     * @param sysOrder 订单
     * @return 订单
     */
    @Override
    public List<SysOrder> selectSysOrderAllList(SysOrder sysOrder)
    {
        return sysOrderMapper.selectSysOrderList(sysOrder);
    }

    /**
     * 新增订单
     * 
     * @param sysOrder 订单
     * @return 结果
     */
    @Override
    public int insertSysOrder(SysOrder sysOrder)
    {
        sysOrder.setOrderCreateDate(new Date());
        sysOrder.setOrderUpdateDate(new Date());
        Random random = new Random();
        String s = "";
        for (int i = 0; i < 10; i++) {
            s = s + random.nextInt(10);
        }
        sysOrder.setOrderNum(new SimpleDateFormat("yyyyMMddHHmm").format(new Date())+s);
        return sysOrderMapper.insertSysOrder(sysOrder);
    }

    /**
     * 修改订单
     * 
     * @param sysOrder 订单
     * @return 结果
     */
    @Override
    public int updateSysOrder(SysOrder sysOrder)
    {
        sysOrder.setOrderUpdateDate(new Date());
        return sysOrderMapper.updateSysOrder(sysOrder);
    }

    /**
     * 删除订单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysOrderByIds(String ids)
    {
        return sysOrderMapper.deleteSysOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单信息
     * 
     * @param orderId 订单ID
     * @return 结果
     */
    @Override
    public int deleteSysOrderById(Long orderId)
    {
        return sysOrderMapper.deleteSysOrderById(orderId);
    }
}
